/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.runtime.component
{
	import com.edi.runtime.control.*;
	
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.core.ClassFactory;
	import mx.core.IFactory;
	import mx.formatters.NumberFormatter;
	import mx.utils.ObjectUtil;
	public class EDIDataGridColumn extends DataGridColumn
	{
		private var _fieldType:String;
		private var _precision:int;
		
		public var numformatter:NumberFormatter=new NumberFormatter();
		//public var useThousandsSeparator:Boolean = true;
		
		public var displayType:String;
		public var fieldLength:Number;	
		public var sumType:String;
		public var staticString:String;
		
		public var table:String;
		public var primaryKey:String="";	
		
		public var index:int;
		public var dataProvider:XMLList;
		public function EDIDataGridColumn(columnName:String=null)
		{
			super(columnName); 
		}
		/**
		 * 对应字段名
		 * @param value
		 * 
		 */		
		public function set field(value:String):void
		{
			dataField=value.toLowerCase();
		}
		public function get field():String
		{
			return dataField;
		}
		/**
		 * 设置字段类型,同时设置排序方法
		 * @param value
		 * 
		 */		
		public function set fieldType(value:String):void
		{
			_fieldType=value;
			if(_fieldType=="string" || _fieldType=="text" || _fieldType=="date")
			{
				this.sortCompareFunction=string_sortCompareFunc;
			}
			if(_fieldType=="int" || _fieldType=="float" || _fieldType=="decimal")
			{
				this.sortCompareFunction=number_sortCompareFunc;
			}
			 
		}
		public function get fieldType():String
		{
			return _fieldType;
		}
		/**
		 * 设置小数位数,向上取整
		 * @param value
		 * 
		 */		
		public function set precision(value:int):void
		{
			this._precision=value;
			numformatter.precision=value;
			numformatter.rounding="up";
			numformatter.useThousandsSeparator=false;
			//EDIApplication.outputDebug("EDIDataGridColumn-->dataProvider.toXMLString(): "+dataProvider.toXMLString());
		}
		public function get precision():int
		{
			return this._precision;
		}
		
//		override public function set labelFunction(value:Function):void{
//			if(this._precision>=0){
//				
//			}
//			super.labelFunction(value);
//		}
		
		
		/**
		 * 按照显示类型返回相应的itemRenderer控件
		 * @return 
		 * 
		 */
		 
		override public function get itemRenderer():IFactory
		{
			var fac:IFactory;
			var obj:Object
			if(dataProvider)
			{
				switch(displayType)
				{
					case "string":
					{
						fac=new ClassFactory(TextItemRenender);
						if(dataProvider)
						{
							ClassFactory(fac).properties=new Object();
							ClassFactory(fac).properties["dataProvider"]=new XMLList(dataProvider.toXMLString());
						}
						break;
					}
					case "boolean":
					{
						fac=new ClassFactory(EDICheckBox);
						obj=(fac as ClassFactory).generator;
						if(obj is EDICheckBox) {
							((fac as ClassFactory).generator as EDICheckBox).table=table;
						}
						break;
					}
					case "image":
					{
						//fac=new ClassFactory(mx.controls.Image);
						fac=new ClassFactory(ImageItemRender);
						ClassFactory(fac).properties=new Object();
						ClassFactory(fac).properties["dataProvider"]=new XMLList(dataProvider.toXMLString());
						//ImageItemRender(EDIClassFactory(fac).generator).dataProvider=dataProvider;
						break;
					}
					case "date":{
						fac=new ClassFactory(DateItemRender);
						ClassFactory(fac).properties=new Object();
						ClassFactory(fac).properties["dataProvider"]=new XMLList(dataProvider.toXMLString());
						break;
					}
					default:
					{
						return super.itemRenderer;
					}					
				}
				return fac;
			}else{
				return super.itemRenderer;

			}

		}
		
		private function number_sortCompareFunc(itemA:Object, itemB:Object):int {
                return ObjectUtil.numericCompare(itemA[field], itemB[field]);
            }
        private function string_sortCompareFunc(itemA:Object, itemB:Object):int {
                return ObjectUtil.stringCompare(itemA[field], itemB[field]);
            }
        private function date_sortCompareFunc(itemA:Object, itemB:Object):int {
                return ObjectUtil.dateCompare(itemA[field], itemB[field]);
            }  
	}
}